package yl.hs.bmipfull.service.production.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.production.IProductionLineDao;
import yl.hs.bmipfull.daos.production.ITransTaskDao;
import yl.hs.bmipfull.pojo.production.ProductionLineEntity;
import yl.hs.bmipfull.service.production.IProductionLineService;
import yl.hs.bmipfull.utils.*;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.RelationType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:  生产线信息表服务层接口实现<br/>
 * 创建时间: 2021年05月25日 15时01分57秒 <br/>
 *
 * @author 张凯
 */
@Service
public class ProductionLineServiceImpl extends ProductionBaseService implements IProductionLineService {

    @Autowired
    IProductionLineDao dao;
    @Autowired
    ITransTaskDao Taskdao;

    @Override
    public List<ProductionLineEntity> queryList(String key) throws Exception {
        return dao.queryListBySearchKey(key,getCompanySNList());
    }

    @Override
    public PageResult<ProductionLineEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductionLineEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<ProductionLineEntity> result = new PageResult(rowNum);
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),ProductionLineEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),getCompanySNList(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),getCompanySNList(), filters));
        return result;
    }


    @Override
    public ProductionLineEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    public Integer saveProductionLine(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProductionLineEntity entity = new ProductionLineEntity();
        if (Tl.isEmpty(sn)) {
            //执行保存

            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setOrganizationCode(getDepartSN());
            entity.setSn(queryNewSN(ProductionLineEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
               var shareCode = writeUnitTag(entity.getSn(),ProductionLineEntity.class, RelationType.CREATE.toString());
                saveGS(
                        entity.getSn(),
                        entity.getPlName(),
                        String.join(",",
                                entity.getPlName(),
                                entity.getPlModel(),
                                entity.getRemark()
                        ),
                        entity,
                        HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                        shareCode
                );
                writeLog(ActionType.CREATE, entity.getSn(), "插入生产线信息表信息", Tl.toJson(entity), ProductionLineEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            var changeLog= Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setOrganizationCode(getDepartSN());
            result = dao.update(entity);
            lineCache.remove(entity.getPlName());
            if (result > 0) {

                writeLog(ActionType.UPDATE, sn, changeLog, Tl.toJson(entity), ProductionLineEntity.class);
            }
        }

        if (result > 0) {

            saveGS(
                    entity.getSn(),
                    entity.getPlName(),
                    String.join(",",
                            entity.getPlName(),
                            entity.getPlModel(),
                            entity.getUseRang(),
                            entity.getRemark()

                    ),
                    entity,
                    HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                    null
            );
        }
        return result;
    }

    @Override
    public Integer delete(String[] sn) {
        var result = dao.changeStateBySn(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, ProductionLineEntity.class);
            deleteGs(sn);
        }
        return result;
    }

    @Override
    public List<ProductionLineEntity> queryListByCate(String cateSN) {
        return dao.queryListByProductCate(cateSN);
    }

    @Override
    public Integer Switch(String productLineSN, String truckSN) {
        var task = Taskdao.queryListByTruckSN(truckSN).get(0);
        if (task != null && task.getState() == 0) {
            task.setProductionLineSN(productLineSN);
            Taskdao.update(task);
            return 1;
        } else {
            return 0;
        }
    }
    private ICache<ProductionLineEntity> lineCache= CacheFactory.getCache();
    @Override
    public ProductionLineEntity findByName(String plName) {
        var line = lineCache.get(plName);
        if(line!=null) {
            return  line;
        }
        else{
            line = dao.findByLineName(plName);
            lineCache.add(plName,line);
        }
        return line;
    }

    @Override
    public List<ProductionLineEntity> queryListByProductCate(String cateSN) {
        return dao.queryListByProductCate(cateSN);
    }

    @Override
    public Integer changeStateBySn(String[] snArray, short state) {
        int result=dao.changeStateBySn(snArray,state);
        if (result > 0) {
            forWriteLog(snArray,state);
        }
        return result;
    }


    @Override
    public List<ProductionLineEntity> getAllLines() {
        return dao.getAll();
    }
}